package com.lsus.golkhadi.client.viewsImpl;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.lsus.golkhadi.client.Golkhadi;
import com.lsus.golkhadi.client.ICardService;
import com.lsus.golkhadi.client.ICardServiceAsync;
import com.lsus.golkhadi.client.images.CardToImageMap;
import com.lsus.golkhadi.client.images.GolkhadiImageBundle;
import com.lsus.golkhadi.client.images.Invoke;
import com.lsus.golkhadi.client.views.ICenterTable;
import com.lsus.golkhadi.shared.Card;
import com.lsus.golkhadi.shared.Position;
import com.lsus.golkhadi.shared.Table;

public class CenterTable extends Composite implements ICenterTable{
	
	final ICardServiceAsync cardService = ( ICardServiceAsync )GWT.create( ICardService.class );
	
	private static CenterTableUiBinder uiBinder = GWT.create(CenterTableUiBinder.class);

	interface CenterTableUiBinder extends UiBinder<Widget, CenterTable> {
	}
	
	private CardImage card1;
	private CardImage card2;
	private CardImage card3;
	private CardImage card4;
	
	@UiField Image logo;
	@UiField VerticalPanel player1Card;
	@UiField HorizontalPanel player2Card;
	@UiField VerticalPanel player3Card;
	@UiField HorizontalPanel player4Card;
	@UiField DockLayoutPanel centerTable;

	public CenterTable() {
		initWidget(uiBinder.createAndBindUi(this));
		logo.setResource(GolkhadiImageBundle.INSTANCE.Logo());
		refreshTable();
	}
	
	Timer parkTimer= new Timer() {
        @Override
        public void run() {
            refreshTable();
        }
    };
    
    //This is the refresh module which is called in certain intervals to update the center table.
    private void refreshTable(){
    	if (LoginPanel.player!=null){
    		cardService.getUpdatedTable(LoginPanel.player.getSessionId(), new AsyncCallback<Table>() {
    			@Override
    			public void onSuccess(Table result) {
    				clearAll();
    				if (result.getPlayer1Card()!=null){
    					card1 = new CardImage(CardToImageMap.getInstance().getMap().get(result.getPlayer1Card()).imageGetter());
    					player1Card.add(card1);
    				}
    				if (result.getPlayer2Card()!=null){
    					card2 = new CardImage(CardToImageMap.getInstance().getMap().get(result.getPlayer2Card()).imageGetter());
    					player2Card.add(card2);
    				}
    				if (result.getPlayer3Card()!=null){
    					card3 = new CardImage(CardToImageMap.getInstance().getMap().get(result.getPlayer3Card()).imageGetter());
    					player3Card.add(card3);
    				}
    				if (result.getPlayer4Card()!=null){
    					card4 = new CardImage(CardToImageMap.getInstance().getMap().get(result.getPlayer4Card()).imageGetter());
    					player4Card.add(card4);
    				}
    				parkTimer.schedule(2000);
    				System.out.println("Refreshing for user :"+LoginPanel.player.getPosition());
    			}

    			@Override
    			public void onFailure(Throwable caught) {
    				Window.alert("Sorry conneciton problem!");
    				parkTimer.schedule(10000);
    			}
    		});
    	}
    	else{
    		parkTimer.schedule(10000);
    	}
    	
    }
	
    //Show card is called when the cards are shown in the table. The card can belong to player in different position, so the position is also passed to this function.
	public boolean showCard(CardImage card, Position p){
		if (p.equals(Position.PLAYER1)){
			if(player1Card.getWidgetCount()<1){
				player1Card.add(card);
				updateTable(card.getCard());
				return true;
			}
		}
		else if(p.equals(Position.PLAYER2)){
			if(player2Card.getWidgetCount()<1){
				player2Card.add(card);
				updateTable(card.getCard());
				return true;
			}
		}
		else if(p.equals(Position.PLAYER3)){
			if(player3Card.getWidgetCount()<1){
				player3Card.add(card);
				updateTable(card.getCard());
				return true;
			}
		}
		else if(p.equals(Position.PLAYER4)){
			if(player4Card.getWidgetCount()<1){
				player4Card.add(card);
				updateTable(card.getCard());
				return true;
			}
		}
		return false;
	}
	
	//clearing UI table
	public void clearAll(){
		player1Card.clear();
		player2Card.clear();
		player3Card.clear();
		player4Card.clear();
	}
	
	//updating table when a card is thrown by the user
	void updateTable(Card card){
		cardService.updateTableWithMyCard(LoginPanel.player, card, new AsyncCallback<Boolean>() {
			@Override
			public void onSuccess(Boolean result) {
				System.out.println("UpdatetableWithMyCardCalled for :"+LoginPanel.player.getPosition());
			}

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Sorry conneciton problem!");
			}
		});
	}
	
	//this is clearing the table 
	@UiHandler("logo")
	void onLogoClick(ClickEvent e){
		cardService.clearTable(LoginPanel.player.getSessionId(), new AsyncCallback<Boolean>() {
			@Override
			public void onSuccess(Boolean result) {
				clearAll();
			}

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Sorry conneciton problem!");
			}
		});
	}
	

	
}
